#######################################################
# 
# views.py
# Python implementation of the Class Views
# Created on:      05-Jun-2008 4:48:55
# Original author: IdealLove
# 
#######################################################
from django.template import Context, loader
from django.http import HttpResponse, HttpResponseRedirect
from google.appengine.api import users
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from gaedbtemplates import models
from gaedbtemplates import forms

class View(object):
    pass

def localization(request, payload):
	if (request.GET):
		payload['locale_selected'] = 'en'
	else:
		payload['locale_selected'] = 'en'
	payload['locale_choices'] = ['ar', 'bg', 'cn', 'hr', 'cs', 
		'en', 'fi', 'fr', 'de', 'el', 'hi', 'it',
		'ja', 'ko', 'no', 'pl', 'pt', 'ro', 'ru', 'es', 'sv'
	]

def user_manager(request, payload):
	payload['current_user'] = users.GetCurrentUser()
	payload['urllogout'] = users.create_logout_url("/")
	payload['urllogin'] = users.create_login_url("/")

def render(request, template, payload):
	localization(request, payload)
	user_manager(request, payload)
	payload['modelmenu'] = models.menu
	return render_to_response(template, payload)


def frontpage(request):
	payload = dict()
	return render(request, 'welcome.html',payload)

def modelview(request, viewtype, modelname, id=None):
	mview = ViewModel(modelname)
	if (viewtype == "modify"):
		return mview.modify(request)
	elif (viewtype == "index"):
		return mview.index(request)
	elif (viewtype == "view"):
		return mview.view(request)
	else:
		return

class ViewModel(View):
	indexprefix = "index"
	modifyprefix = "modify"
	viewprefix = "view"
	modelname=""
	model = models.DBModel()
	modelform = models.DBModelForm()
	def __init__(self, modelname=""):
		self.modelname = modelname
		self.model = eval("models.%s()" % self.modelname)
	def index(self, request):
		model_index = eval("models.%s.objects.all()[:25]" % self.modelname)
		template = self.indexprefix + "_model.html"
		model_set = list()
		modeldict =set()
		if (len(model_index) > 0 ):
			tmodeldict=model_index[0].__dict__
			for modelfield in tmodeldict:
				if (not modelfield in set(["_parent","_app", "_entity"] )):
					modeldict.add(modelfield[1:])
			for model in model_index:
				smodel = dict()
				for field in modeldict:
					smodel[field] = model.__getattribute__(field)
					print(smodel[field])
					model_set.append(smodel)
		payload = dict(model_index = model_set, modeldict = modeldict, modelname = self.modelname)
		return render(request, template, payload)
		pass
	def modify(self, request):
		
		if (request.POST):
			self.modelform = eval("models.%sForm(data=request.POST)" % self.modelname)
			if self.modelform.is_valid():
				dbObj = self.modelform.save()
#				print(dbObj)
		else:
			self.modelform = eval("models.%sForm()" % self.modelname)
		template = self.modifyprefix + "_model.html"
		payload = dict( form = self.modelform , modelname = self.modelname)
		return render(request, template, payload)
		pass
	def view(self, request):
		pass
	